#pragma once
#include"Stack.h"

void StackInit(Stack* ST)
{
	assert(ST);
	ST->arr = (StackDataType*)malloc(4 * sizeof(StackDataType));
	ST->capacity = 4;
	ST->top = 0;
}

void StackDestroy(Stack* ST)
{
	free(ST->arr);
	ST->arr = NULL;
	ST->capacity = ST->top = 0;
}

void StackPrint(Stack ST)
{
	assert(ST.arr);
	for (int i = 0; i < ST.top; i++)
	{
		printf("%d ", ST.arr[i]);
	}
	printf("\n");
}

void StackPush(Stack* ST, StackDataType data)
{
	assert(ST);
	if (ST->top == ST->capacity)
	{
		StackDataType* tmp = realloc(ST->arr, 2 * sizeof(ST->arr));
		if (tmp == NULL)
		{
			perror("realloc:");
		}
		ST->arr = tmp;
		ST->capacity = 2 * (ST->capacity);
	}
	ST->arr[ST->top++] = data;
	ST->capacity++;
}

void StackPop(Stack* ST, StackDataType data)
{
	assert(ST);
	ST->top--;
}

StackDataType StackTop(Stack* ST)
{
	assert(ST);
	return ST->arr[ST->top - 1];
}

bool StackEmpty(Stack* ST)
{
	assert(ST->arr);
	return ST->top == 0;
}

int STSize(Stack* pst)
{
	assert(pst);

	return pst->top;
}